package org.example.back.controller.advice;

import org.example.back.service.advice.UserMealAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/advice")
public class UserMealAdviceController {

    @Autowired
    private UserMealAdviceService adviceService;

    // 日建议，日期可选，默认当天或昨天
    @GetMapping("/day")
    public String getDailyAdvice(@RequestParam(required = false) String date, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        LocalDate queryDate = (date != null) ? LocalDate.parse(date) : LocalDate.now();
        return adviceService.getDailyAdvice(userId, queryDate);
    }

    // 周建议，从session拿userId
    @GetMapping("/week")
    public String getWeeklyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return adviceService.getWeeklyAdvice(userId);
    }

    // 月建议，从session拿userId
    @GetMapping("/month")
    public String getMonthlyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return adviceService.getMonthlyAdvice(userId);
    }

}
